
app.controller('SignAuthCtrl',function ($scope,$http,$state,$localStorage,$location,$window) {    
    $scope.namedata = $localStorage.cur_user.uname;
    $scope.nn = [];
    $http.post(
        $scope.app.backendurl+"admin-user/view-rights",
        {
           
            'user_id': $state.params.userid,                             
            'token':$localStorage.token                   

        }
    ).success(function (data) {
      //console.log(data);
      if(data.code==0&&data.msg=="成功"){
        $scope.aaData = data.data.role_right;
        $scope.mmData = data.data.layer_right;
        $scope.nn = data.data.layers;

      }
       if(data.code==20001){
         $location.path('access.signin');
       }
    }).error(function (data) {
        alert(data);
    })

    
     
    // $scope.hh = [];  
    $scope.c = {};
    $scope.add = function(){
      $scope.c = $scope.b;
      // $scope.hh.push($scope.b);
     if($scope.c.school_name!="所有学校"){      
      if($scope.c.buildingtype!=null&&$scope.c.buildingtype!=undefined&&$scope.c.buildingtype!=""){
        if($scope.c.building_name!=null&&$scope.c.building_name!=undefined&&$scope.c.building_name!=""){
          if($scope.c.layer_name!=null&&$scope.c.layer_name!=undefined&&$scope.c.layer_name!=""){
            if($scope.c.room_name!=null&&$scope.c.room_name!=undefined&&$scope.c.room_name!=""){
              $scope.c.school_name = $scope.c.school_name;
              $scope.c.school_id = $scope.mmData[$scope.c.school_name].school_id;
              $scope.c.buildingtype = $scope.c.buildingtype.split('+')[0];
              $scope.c.campus_id = $scope.c.layer_name.split('+')[1];
              $scope.c.building_name = $scope.c.building_name;
              $scope.c.layer_id = $scope.c.layer_name.split('+')[2]; 
              $scope.c.layer_name = $scope.c.layer_name.split('+')[0];
              
              $scope.c.room_id = $scope.c.room_name.split(',')[1];
              $scope.c.room_name = $scope.c.room_name.split(',')[0];
            }else{
              $scope.c.school_name = $scope.c.school_name;
              $scope.c.school_id = $scope.mmData[$scope.c.school_name].school_id;
              $scope.c.buildingtype = $scope.c.buildingtype.split('+')[0];
              $scope.c.campus_id = $scope.c.layer_name.split('+')[1];
              $scope.c.building_name = $scope.c.building_name;
              $scope.c.layer_name = $scope.c.layer_name.split('+')[0];
              $scope.c.layer_id = $scope.c.layer_name.split('+')[2];               
              $scope.c.room_name = "所有室";
              $scope.c.room_id = "" 

            }

          }else{
            $scope.c.school_name = $scope.c.school_name;
            $scope.c.school_id = $scope.mmData[$scope.c.school_name].school_id;
            $scope.c.buildingtype = $scope.c.buildingtype.split('+')[0];
            $scope.c.campus_id = $scope.mmData[$scope.c.school_name][$scope.c.buildingtype.split('+')[0]][$scope.c.building_name].campus_id;
            $scope.c.building_name = $scope.c.building_name;            
            $scope.c.layer_name = "所有层";
            $scope.c.layer_id = "";
            $scope.c.room_id = "";
            $scope.c.room_name = "";

          }


        }else{
          $scope.c.school_name = $scope.c.school_name;
          $scope.c.school_id = $scope.mmData[$scope.c.school_name].school_id;
          $scope.c.buildingtype = $scope.c.buildingtype.split('+')[0];          
          $scope.c.building_name = "所有楼";
          $scope.c.campus_id = "";
          $scope.c.layer_id = "" ;
          $scope.c.layer_name = "" ;
          $scope.c.room_id = "";
          $scope.c.room_name = "";
        }

      }else{
        $scope.c.school_name = $scope.c.school_name;
        $scope.c.buildingtype = "所有类型";
        $scope.c.school_id = $scope.mmData[$scope.c.school_name].school_id;
        $scope.c.campus_id = "";
        $scope.c.building_name = "";
        $scope.c.layer_id = "" ;
        $scope.c.layer_name = "" ;
        $scope.c.room_id = "";
        $scope.c.room_name = "";

      }
      
      
    }else{
      $scope.c.school_name = $scope.c.school_name;
      $scope.c.school_id = "";
      $scope.c.buildingtype = "";
      $scope.c.campus_id = "";
      $scope.c.building_name = "";
      $scope.c.layer_id = "" ;
      $scope.c.layer_name = "" ;
      $scope.c.room_id = "";
      $scope.c.room_name = "";
    }
      
      for(var key in $scope.c){
        if($scope.c[key]==undefined){
          $scope.c[key] = ''
        }
      }
      $scope.nn.push($scope.c);  
      $scope.b = {};
      $scope.c = {};
    }

    $scope.getrooms = function(cid){
      $scope.b.layer_name='';
      if(cid!=null&&cid!= undefined&&cid!=''){
        $http.post(
        $scope.app.backendurl+"tbl-campus-building/get-rooms",
            {
               
                'campus_building_id': cid,                             
                'token':$localStorage.token                   

            }
        ).success(function (data) {
          //console.log(data);
          if(data.code==0&&data.msg=="成功"){
            $scope.roomData = data.data            

          }
          if(data.code==1){
            alert(data.msg);
          }
           if(data.code==20001){
             $location.path('access.signin');
           }
        }).error(function (data) {
            alert(data);
        })
      }
       
    }

    $scope.del = function(q){
      var index = q;    
      $scope.nn.splice(index, 1);
    }
    $scope.save = function(){
          var m = $scope.nn;      
          var thdata=[]; var str =""; 
          $("input[name='cbx']:checked").each(function(){
             thdata.push($(this).val());
          });
          str = thdata.join(';'); 
         
          $("input[name='cbx']").change(function() {      
            if($(this).is(":checked")){
              thdata.push($(this).val());
            }else{
              delete thdata[thdata.indexOf($(this).val())];
            }
            thdata = thdata.filter(thdata => thdata!=undefined && thdata!==null);   
            str = thdata.join(';');
          });
      var tdata = str.split(';');
      var authlist = {};
      authlist.roleids = tdata;
     
      authlist.layers = m;      
      //console.log(authlist);
      var jsondata = JSON.stringify(authlist);
      //console.log(jsondata);

      $http.post(
          $scope.app.backendurl+"admin-user/update-rights",
          {
             
              'user_id': $state.params.userid,  
              'data': jsondata,                          
              'token':$localStorage.token                   

          }
      ).success(function (data) {
        //console.log(data);
        if(data.code==0&&data.msg=="成功"){
          alert(data.msg);
          $window.location.reload('app.sys.usersignauth')
        }
        if(data.code==1){
          alert(data.msg)
        }
         if(data.code==20001){
           $location.path('access.signin');
         }
      }).error(function (data) {
          alert(data);
      })

    }
  })
